package com.greatwqs.wmall.common.provider.config;

import com.greatwqs.wmall.common.provider.cache.SpringCacheConfig;

import lombok.extern.slf4j.Slf4j;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * Redis config all details.
 *
 * @author wang-qingsong
 * @see org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration
 */
@Slf4j
@Configuration
public class RedisConfig {

    @Primary
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        log.warn("RedisConfig redisTemplate init, factory: " + factory.getClass());
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(factory);
        redisTemplate.setKeySerializer(SpringCacheConfig.stringRedisSerializer());
        redisTemplate.setValueSerializer(SpringCacheConfig.genericJackson2JsonRedisSerializer());
        redisTemplate.setHashKeySerializer(SpringCacheConfig.stringRedisSerializer());
        redisTemplate.setHashValueSerializer(SpringCacheConfig.genericJackson2JsonRedisSerializer());
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}
